"""
config/resp.py

the flask_restful wrapper
"""
from config.const import ResponseCodeEnum
from flask import jsonify, make_response

CODE = {
    0: 'ok',
    403: 'access define',
    400: 'bad request',
    401: 'unauthorized',
    404: 'not found',
    500: 'internal  error'
}


def get_resp(errcode, errmsg, data, code):
    resp = {}
    resp['data'] = data
    resp['errcode'] = errcode
    resp['errmsg'] = errmsg if errmsg else CODE.get(errcode, 'sorry, you need custom this errmsg and errcode')
    resp = make_response(jsonify(resp), code if code is not None else 200)
    return resp


class Resp:
    """状态码为2xx时调用此类"""

    @staticmethod
    def ok(errcode=ResponseCodeEnum.ZERO.value, errmsg=None, data=None, code=None):
        resp = get_resp(errcode, errmsg, data, code)
        return resp

    @staticmethod
    def forbidden(errcode=ResponseCodeEnum.FORBIDDEN.value, errmsg=None, data=None, code=None):
        resp = get_resp(errcode, errmsg, data, code)
        return resp

    @staticmethod
    def bad_request(errcode=ResponseCodeEnum.BAD_REQUEST.value, errmsg=None, data=None, code=None):
        resp = get_resp(errcode, errmsg, data, code)
        return resp

    @staticmethod
    def not_found(errcode=ResponseCodeEnum.NOT_FOUND.value, errmsg=None, data=None, code=404):
        resp = get_resp(errcode, errmsg, data, code)
        return resp